package net.chasing.androidbaseconfig.adapter.recycleradaper;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.text.TextWatcher;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.Checkable;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import net.chasing.androidbaseconfig.util.ViewUtils;

public class RecyclerViewHelper {

    private final Context context;
    public View itemView;
    public int position;

    protected RecyclerViewHelper(Context context, View itemView, int position) {
        this.context = context;
        this.itemView = itemView;
        this.position = position;
    }

    public <T extends View> T getView(int viewId) {
        return retrieveView(viewId);
    }

    private <T extends View> T retrieveView(int viewId) {
        return itemView.findViewById(viewId);
    }

    // <editor-fold desc="属性设置">
    public RecyclerViewHelper setText(int viewId, String value) {
        if (value == null) return this;
        TextView view = retrieveView(viewId);
        view.setText(value);
        return this;
    }

    public RecyclerViewHelper setMaxLines(int viewId, int maxLines) {
        TextView view = retrieveView(viewId);
        view.setMaxLines(maxLines);
        return this;
    }

    public RecyclerViewHelper setEnable(int viewId, boolean enable) {
        View view = retrieveView(viewId);
        view.setEnabled(enable);
        return this;
    }

    public RecyclerViewHelper setText(int viewId, int resId) {
        TextView view = retrieveView(viewId);
        view.setText(resId);
        return this;
    }

    public RecyclerViewHelper setHint(int viewId, String value) {
        EditText view = retrieveView(viewId);
        view.setHint(value);
        return this;
    }

    public RecyclerViewHelper setViewHeight(int viewId, int viewHeight) {
        View view = retrieveView(viewId);
        ViewGroup.LayoutParams lp = view.getLayoutParams();
        lp.height = viewHeight;
        view.setLayoutParams(lp);
        return this;
    }

    public RecyclerViewHelper setViewMinHeight(int viewId, int minHeight) {
        View view = retrieveView(viewId);
        view.setMinimumHeight(minHeight);
        return this;
    }

    public RecyclerViewHelper setViewWidth(int viewId, int width) {
        View view = retrieveView(viewId);
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        layoutParams.width = width;
        view.setLayoutParams(layoutParams);
        return this;
    }

    public RecyclerViewHelper setViewWidthHeight(int viewId, int width, int height) {
        View view = retrieveView(viewId);
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        layoutParams.width = width;
        layoutParams.height = height;
        view.setLayoutParams(layoutParams);
        return this;
    }

    public RecyclerViewHelper setImageResource(int viewId, int imageResId) {
        ImageView view = retrieveView(viewId);
        view.setImageResource(imageResId);
        return this;
    }

    public RecyclerViewHelper setBackgroundColor(int viewId, int color) {
        View view = retrieveView(viewId);
        view.setBackgroundColor(color);
        return this;
    }

    public RecyclerViewHelper setBackgroundRes(int viewId, int backgroundRes) {
        View view = retrieveView(viewId);
        view.setBackgroundResource(backgroundRes);
        return this;
    }

    public RecyclerViewHelper setBackgroundDrawable(int viewId, Drawable drawable) {
        View view = retrieveView(viewId);
        view.setBackground(drawable);
        return this;
    }

    public RecyclerViewHelper setTextColor(int viewId, int textColor) {
        TextView view = retrieveView(viewId);
        view.setTextColor(textColor);
        return this;
    }

    public RecyclerViewHelper setTextColorRes(int viewId, int textColorRes) {
        TextView view = retrieveView(viewId);
        view.setTextColor(ContextCompat.getColor(context, textColorRes));
        return this;
    }

    public RecyclerViewHelper setHighLightTextColorRes(int viewId, int textColorRes) {
        TextView view = retrieveView(viewId);
        view.setHighlightColor(ContextCompat.getColor(context, textColorRes));
        return this;
    }

    public RecyclerViewHelper setTextHintColorRes(int viewId, int textColorRes) {
        EditText view = retrieveView(viewId);
        view.setHintTextColor(ContextCompat.getColor(context, textColorRes));
        return this;
    }

    public RecyclerViewHelper setImageDrawable(int viewId, Drawable drawable) {
        ImageView view = retrieveView(viewId);
        view.setImageDrawable(drawable);
        return this;
    }

    public RecyclerViewHelper setImageBitmap(int viewId, Bitmap bitmap) {
        ImageView view = retrieveView(viewId);
        view.setImageBitmap(bitmap);
        return this;
    }

    public RecyclerViewHelper setImageScaleType(int viewId, ImageView.ScaleType scaleType) {
        ImageView view = retrieveView(viewId);
        view.setScaleType(scaleType);
        return this;
    }

    public RecyclerViewHelper setAlpha(int viewId, float value) {
        retrieveView(viewId).setAlpha(value);
        return this;
    }

    public RecyclerViewHelper setTextViewMaxWidth(int viewId, int maxWidth) {
        View view = retrieveView(viewId);
        if (view instanceof TextView)
            ((TextView) view).setMaxWidth(maxWidth);
        return this;
    }

    public RecyclerViewHelper setVisible(int viewId, boolean visible) {
        View view = retrieveView(viewId);
        if (view != null)
            view.setVisibility(visible ? View.VISIBLE : View.GONE);
        return this;
    }

    public RecyclerViewHelper setVisibleAndInvisible(int viewId, boolean visible) {
        View view = retrieveView(viewId);
        view.setVisibility(visible ? View.VISIBLE : View.INVISIBLE);
        return this;
    }

    public RecyclerViewHelper setTextViewDeleteLine(int viewId, boolean isAddDeleteLine) {
        TextView view = retrieveView(viewId);
        if (isAddDeleteLine)
            view.setPaintFlags(view.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
        else
            view.setPaintFlags(view.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));
        return this;
    }

    public RecyclerViewHelper setProgress(int viewId, int progress) {
        ProgressBar view = retrieveView(viewId);
        view.setProgress(progress);
        return this;
    }

    public RecyclerViewHelper setProgress(int viewId, int progress, int max) {
        ProgressBar view = retrieveView(viewId);
        view.setMax(max);
        view.setProgress(progress);
        return this;
    }

    public RecyclerViewHelper setProgressDrawable(int viewId, int resId) {
        ProgressBar view = retrieveView(viewId);
        view.setProgressDrawable(ContextCompat.getDrawable(context, resId));
        return this;
    }

    //注意：不能为0,0则会显示默认的5个
    public RecyclerViewHelper setNumStars(int viewId, int numStars) {
        RatingBar view = retrieveView(viewId);
        view.setNumStars(numStars);
        return this;
    }

    public RecyclerViewHelper setRating(int viewId, float rating) {
        RatingBar view = retrieveView(viewId);
        view.setRating(rating);
        return this;
    }

    public RecyclerViewHelper setTag(int viewId, Object tag) {
        View view = retrieveView(viewId);
        view.setTag(tag);
        return this;
    }

    public RecyclerViewHelper setTag(int viewId, int key, Object tag) {
        View view = retrieveView(viewId);
        view.setTag(key, tag);
        return this;
    }

    public RecyclerViewHelper setTextGravity(int viewId, int gravity) {
        View view = retrieveView(viewId);
        if (view instanceof TextView)
            ((TextView) view).setGravity(gravity);
        return this;
    }

    public RecyclerViewHelper setChecked(int viewId, boolean checked) {
        Checkable view = retrieveView(viewId);
        view.setChecked(checked);
        return this;
    }

    public RecyclerViewHelper setAdapter(int viewId, Adapter adapter) {
        AdapterView view = retrieveView(viewId);
        view.setAdapter(adapter);
        return this;
    }

    public RecyclerViewHelper setMargin(int viewId, int startMargin, int topMargin,
                                        int endMargin, int bottomMargin) {
        View v = retrieveView(viewId);
        if (v.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
            ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
            layoutParams.setMarginStart(startMargin);
            layoutParams.topMargin = topMargin;
            layoutParams.setMarginEnd(endMargin);
            layoutParams.bottomMargin = bottomMargin;
            v.setLayoutParams(layoutParams);
        } else {
            Log.e("RecyclerHelper", "This LayoutParams is not instanceof ViewGroup.MarginLayoutParams");
        }
        return this;
    }

    public RecyclerViewHelper setMarginTop(int viewId, int margin) {
        View v = retrieveView(viewId);
        if (v.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
            ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
            layoutParams.topMargin = margin;
            v.setLayoutParams(layoutParams);
        } else {
            Log.e("RecyclerHelper", "This LayoutParams is not instanceof ViewGroup.MarginLayoutParams");
        }
        return this;
    }

    public RecyclerViewHelper setMarginBottom(int viewId, int margin) {
        View v = retrieveView(viewId);
        if (v.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
            ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
            layoutParams.bottomMargin = margin;
            v.setLayoutParams(layoutParams);
        } else {
            Log.e("RecyclerHelper", "This LayoutParams is not instanceof ViewGroup.MarginLayoutParams");
        }
        return this;
    }

    public RecyclerViewHelper setMarginStart(int viewId, int margin) {
        View v = retrieveView(viewId);
        if (v.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
            ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
            layoutParams.setMarginStart(margin);
            v.setLayoutParams(layoutParams);
        } else {
            Log.e("RecyclerHelper", "This LayoutParams is not instanceof ViewGroup.MarginLayoutParams");
        }
        return this;
    }

    public RecyclerViewHelper setMarginEnd(int viewId, int margin) {
        View v = retrieveView(viewId);
        if (v.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
            ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
            layoutParams.setMarginEnd(margin);
            v.setLayoutParams(layoutParams);
        } else {
            Log.e("RecyclerHelper", "This LayoutParams is not instanceof ViewGroup.MarginLayoutParams");
        }
        return this;
    }

    public RecyclerViewHelper setPadding(int viewId, int start, int top, int end, int bottom) {
        View v = retrieveView(viewId);
        v.setPadding(start, top, end, bottom);
        return this;
    }

    public RecyclerViewHelper setPaddingStart(int viewId, int paddingStart) {
        View v = retrieveView(viewId);
        int paddingTop = v.getPaddingTop();
        int paddingEnd = v.getPaddingEnd();
        int paddingBottom = v.getPaddingBottom();
        v.setPadding(paddingStart, paddingTop, paddingEnd, paddingBottom);
        return this;
    }

    public RecyclerViewHelper setPaddingTop(int viewId, int paddingTop) {
        View v = retrieveView(viewId);
        int paddingStart = v.getPaddingStart();
        int paddingEnd = v.getPaddingEnd();
        int paddingBottom = v.getPaddingBottom();
        v.setPadding(paddingStart, paddingTop, paddingEnd, paddingBottom);
        return this;
    }

    public RecyclerViewHelper setPaddingEnd(int viewId, int paddingEnd) {
        View v = retrieveView(viewId);
        int paddingStart = v.getPaddingStart();
        int paddingTop = v.getPaddingTop();
        int paddingBottom = v.getPaddingBottom();
        v.setPadding(paddingStart, paddingTop, paddingEnd, paddingBottom);
        return this;
    }

    public RecyclerViewHelper setPaddingBottom(int viewId, int paddingBottom) {
        View v = retrieveView(viewId);
        int paddingStart = v.getPaddingStart();
        int paddingTop = v.getPaddingTop();
        int paddingEnd = v.getPaddingEnd();
        v.setPadding(paddingStart, paddingTop, paddingEnd, paddingBottom);
        return this;
    }

    public RecyclerViewHelper setTextViewDrawablePadding(int viewId, int padding) {
        TextView view = retrieveView(viewId);
        view.setCompoundDrawablePadding(padding);
        return this;
    }

    public RecyclerViewHelper setTextSize(int viewId, int textSize) {
        View view = retrieveView(viewId);
        if (view instanceof TextView)
            ((TextView) view).setTextSize(ViewUtils.isUseDp ? TypedValue.COMPLEX_UNIT_DIP : TypedValue.COMPLEX_UNIT_SP, textSize);
        return this;
    }

    public RecyclerViewHelper drawable(int viewId, int start, int top, int end, int bottom) {
        View view = retrieveView(viewId);
        if (view instanceof TextView)
            try {
                ((TextView) view).setCompoundDrawablesWithIntrinsicBounds(
                        start > 0 ? ContextCompat.getDrawable(context, start) : null,
                        top > 0 ? ContextCompat.getDrawable(context, top) : null,
                        end > 0 ? ContextCompat.getDrawable(context, end) : null,
                        bottom > 0 ? ContextCompat.getDrawable(context, bottom) : null);
            } catch (Resources.NotFoundException e) {
                e.printStackTrace();
            }
        return this;
    }

    public RecyclerViewHelper drawableStart(int viewId, int drawableId) {
        View view = retrieveView(viewId);
        if (view instanceof TextView)
            try {
                ((TextView) view).setCompoundDrawablesWithIntrinsicBounds(drawableId > 0 ? ContextCompat.getDrawable(context, drawableId) : null, null, null, null);
            } catch (Resources.NotFoundException e) {
                e.printStackTrace();
            }
        return this;
    }

    public RecyclerViewHelper drawableTop(int viewId, int drawableId) {
        View view = retrieveView(viewId);
        if (view instanceof TextView)
            try {
                ((TextView) view).setCompoundDrawablesWithIntrinsicBounds(null, drawableId > 0 ? ContextCompat.getDrawable(context, drawableId) :
                        null, null, null);
            } catch (Resources.NotFoundException e) {
                e.printStackTrace();
            }
        return this;
    }

    public RecyclerViewHelper drawableEnd(int viewId, int drawableId) {
        View view = retrieveView(viewId);
        if (view instanceof TextView)
            try {
                ((TextView) view).setCompoundDrawablesWithIntrinsicBounds(null, null, drawableId > 0 ? ContextCompat.getDrawable(context, drawableId) : null, null);
            } catch (Resources.NotFoundException e) {
                e.printStackTrace();
            }
        return this;
    }

    public RecyclerViewHelper drawableBottom(int viewId, int drawableId) {
        View view = retrieveView(viewId);
        if (view instanceof TextView)
            try {
                ((TextView) view).setCompoundDrawablesWithIntrinsicBounds(null, null, null, drawableId > 0 ? ContextCompat.getDrawable(context, drawableId) : null);
            } catch (Resources.NotFoundException e) {
                e.printStackTrace();
            }
        return this;
    }

    public RecyclerViewHelper setTranslationX(int viewId, float translationX) {
        View view = retrieveView(viewId);
        view.setTranslationX(translationX);
        return this;
    }

    public RecyclerViewHelper setLayoutParams(int viewId, ViewGroup.LayoutParams layoutParams) {
        retrieveView(viewId).setLayoutParams(layoutParams);
        return this;
    }
    // </editor-fold>

    // <editor-fold desc="属性获取">
    public long getTextLine(int viewId) {
        View view = retrieveView(viewId);
        if (view instanceof TextView) {
            return ((TextView) view).getLineCount();
        }
        return 0;
    }

    public boolean isVisible(int viewId) {
        View view = retrieveView(viewId);
        return view.getVisibility() == View.VISIBLE;
    }

    public float getX(int viewId) {
        return retrieveView(viewId).getX();
    }

    public float getY(int viewId) {
        return retrieveView(viewId).getY();
    }

    public int getLeft(int viewId) {
        return retrieveView(viewId).getLeft();
    }

    public int getTop(int viewId) {
        return retrieveView(viewId).getTop();
    }

    public int getBottom(int viewId) {
        return retrieveView(viewId).getBottom();
    }

    public int getPaddingStart(int viewId) {
        return retrieveView(viewId).getPaddingStart();
    }

    public int getPaddingEnd(int viewId) {
        return retrieveView(viewId).getPaddingEnd();
    }

    public int getPaddingBottom(int viewId) {
        return retrieveView(viewId).getPaddingBottom();
    }

    public int getHeight(int viewId) {
        View v = retrieveView(viewId);
        int height = v.getHeight();
        if (height <= 0) {
            int size = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
            v.measure(size, size);
            height = v.getMeasuredHeight();
        }
        return height;
    }

    public int getWidth(int viewId) {
        View v = retrieveView(viewId);
        int width = v.getWidth();
        if (width <= 0) {
            int size = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
            v.measure(size, size);
            width = v.getMeasuredWidth();
        }
        return width;
    }

    public String getText(int viewId) {
        return ((TextView) retrieveView(viewId)).getText().toString().trim();
    }

    public boolean isCheckBoxChecked(int viewId) {
        CheckBox view = getView(viewId);
        return view.isChecked();
    }

    public void post(int viewId, Runnable runnable) {
        getView(viewId).post(runnable);
    }

    public ViewGroup.LayoutParams getLayoutParams(int viewId) {
        return retrieveView(viewId).getLayoutParams();
    }
    // </editor-fold>

    // <editor-fold desc="监听器类">
    public RecyclerViewHelper setOnClickListener(int viewId, View.OnClickListener listener) {
        View view = retrieveView(viewId);
        view.setOnClickListener(listener);
        return this;
    }

    public RecyclerViewHelper setOnClickListener(int[] viewIds, View.OnClickListener listener) {
        if (viewIds != null && viewIds.length > 0) {
            for (int viewId : viewIds) {
                View view = retrieveView(viewId);
                view.setOnClickListener(listener);
            }
        }
        return this;
    }

    public RecyclerViewHelper setOnCheckedListener(int viewId, CompoundButton.OnCheckedChangeListener listener) {
        CheckBox view = retrieveView(viewId);
        view.setOnCheckedChangeListener(listener);
        return this;
    }

    public RecyclerViewHelper setOnTouchListener(int viewId, View.OnTouchListener listener) {
        View view = retrieveView(viewId);
        view.setOnTouchListener(listener);
        return this;
    }

    public RecyclerViewHelper setOnLongClickListener(int viewId, View.OnLongClickListener listener) {
        View view = retrieveView(viewId);
        view.setOnLongClickListener(listener);
        return this;
    }

    public RecyclerViewHelper setOnFocusChangeListener(int viewId, View.OnFocusChangeListener listener) {
        View view = retrieveView(viewId);
        view.setOnFocusChangeListener(listener);
        return this;
    }

    public RecyclerViewHelper addTextChangeListener(int viewId, TextWatcher textWatcher) {
        View view = retrieveView(viewId);
        if (view instanceof TextView) {
            ((TextView) view).addTextChangedListener(textWatcher);
        }
        return this;
    }

    public RecyclerViewHelper removeTextChangeListener(int viewId, TextWatcher textWatcher) {
        View view = retrieveView(viewId);
        if (view instanceof TextView) {
            ((TextView) view).removeTextChangedListener(textWatcher);
        }
        return this;
    }

    public RecyclerViewHelper setOnEditorActionListener(int viewId, TextView.OnEditorActionListener listener) {
        View view = retrieveView(viewId);
        if (view instanceof TextView) {
            ((TextView) view).setOnEditorActionListener(listener);
        }
        return this;
    }
    // </editor-fold>

    public void performClick(int viewId) {
        View view = retrieveView(viewId);
        view.performClick();
    }
}
